<?php
class Message extends CI_Controller {
	function __construct() {
		parent::__construct ();
		$this->load->helper ( 'url' );
		$this->load->library ( 'form_validation' );
	}
	
	function joinList() {
		if (($this->session->userdata ( 'authority' ) === '0') || ($this->session->userdata ( 'authority' ) === '1') || ($this->session->userdata ( 'authority' ) === '2')) {
			$this->load->model ( 'Mmessage' );
			$this->load->library ( 'pagination' );
			
			$data ['search'] = '';
			$search = '';
			
			if ($this->input->post ( 'search' )) {
				$data ['search'] = preg_replace ( "/[^ " . chr ( 0x80 ) . "-" . chr ( 0xff ) . "\\w]+/ ", ' ', $this->input->post ( 'search' ) );
				if ($data ['search'] != '') {
					$searchRow = explode ( ' ', $data ['search'] );
					$search = '(';
					foreach ( $searchRow as $row ) {
						if ($row) {
							$search .= "companyName like '%" . $row . "%' OR ";
							$search .= "name like '%" . $row . "%' OR ";
							$search .= "mobile like '%" . $row . "%' OR ";
						}
					}
					$search = substr ( $search, 0, strlen ( $search ) - 3 );
					if ($search)
						$search .= ')';
				}
			}
			
			$data ['listMode'] = $this->uri->segment ( 4, 0 );
			$data ['page'] = $this->uri->segment ( 5, 0 );
			$data ['num'] = $this->Mmessage->getBusinessJoinNum ( $data ['listMode'], $search );
			
			$config ['base_url'] = base_url () . '/admin/message/joinList/' . $data ['listMode'] . '/';
			$config ['total_rows'] = $data ['num'];
			$config ['per_page'] = PAGE_SHOW_NUM;
			$config ['uri_segment'] = 5;
			$config ['num_links'] = 2;
			$this->pagination->initialize ( $config );
			
			$data ['list'] = $this->Mmessage->getBusinessJoinList ( $data ['listMode'], $data ['page'], PAGE_SHOW_NUM, $search );
			
			$data ['authority'] = $this->session->userdata ( 'authority' );
			$data ['adminName'] = $this->session->userdata ( 'adminName' );
			
			$this->load->view ( 'admin/header', $data );
			$this->load->view ( 'admin/nav' );
			$this->load->view ( 'admin/message_joinlist' );
			$this->load->view ( 'admin/footer' );
		} else {
			$data ['msg'] = '请先登录！';
			$data ['url'] = base_url () . 'admin/home/login';
			$this->load->view ( 'admin/admin_message', $data );
		}
	}
	
	function reservationList() {
		if (($this->session->userdata ( 'authority' ) === '0') || ($this->session->userdata ( 'authority' ) === '1') || ($this->session->userdata ( 'authority' ) === '2')) {
			$this->load->model ( 'Mmessage' );
			$this->load->library ( 'pagination' );
			
			$data ['search'] = '';
			$search = '';
			
			if ($this->input->post ( 'search' )) {
				$data ['search'] = preg_replace ( "/[^ " . chr ( 0x80 ) . "-" . chr ( 0xff ) . "\\w]+/ ", ' ', $this->input->post ( 'search' ) );
				if ($data ['search'] != '') {
					$searchRow = explode ( ' ', $data ['search'] );
					$search = '(';
					foreach ( $searchRow as $row ) {
						if ($row) {
							$search .= "cardId like '%" . $row . "%' OR ";
							$search .= "name like '%" . $row . "%' OR ";
							$search .= "mobile like '%" . $row . "%' OR ";
						}
					}
					$search = substr ( $search, 0, strlen ( $search ) - 3 );
					if ($search)
						$search .= ')';
				}
			}
			
			$data ['listMode'] = $this->uri->segment ( 4, 0 );
			$data ['page'] = $this->uri->segment ( 5, 0 );
			$data ['num'] = $this->Mmessage->getReservationNum ( $data ['listMode'], $search );
			
			$config ['base_url'] = base_url () . '/admin/message/reservationList/' . $data ['listMode'] . '/';
			$config ['total_rows'] = $data ['num'];
			$config ['per_page'] = PAGE_SHOW_NUM;
			$config ['uri_segment'] = 5;
			$config ['num_links'] = 2;
			$this->pagination->initialize ( $config );
			
			$data ['list'] = $this->Mmessage->getReservationList ( $data ['listMode'], $data ['page'], PAGE_SHOW_NUM, $search );
			
			$data ['authority'] = $this->session->userdata ( 'authority' );
			$data ['adminName'] = $this->session->userdata ( 'adminName' );
			
			$this->load->view ( 'admin/header', $data );
			$this->load->view ( 'admin/nav' );
			$this->load->view ( 'admin/message_reservationlist' );
			$this->load->view ( 'admin/footer' );
		} else {
			$data ['msg'] = '请先登录！';
			$data ['url'] = base_url () . 'admin/home/login';
			$this->load->view ( 'admin/admin_message', $data );
		}
	}
	
	function messageList() {
		if (($this->session->userdata ( 'authority' ) === '0') || ($this->session->userdata ( 'authority' ) === '1') || ($this->session->userdata ( 'authority' ) === '2')) {
			$this->load->model ( 'Mmessage' );
			$this->load->library ( 'pagination' );
			
			$data ['search'] = '';
			$search = '';
			
			if ($this->input->post ( 'search' )) {
				$data ['search'] = preg_replace ( "/[^ " . chr ( 0x80 ) . "-" . chr ( 0xff ) . "\\w]+/ ", ' ', $this->input->post ( 'search' ) );
				if ($data ['search'] != '') {
					$searchRow = explode ( ' ', $data ['search'] );
					$search = '(';
					foreach ( $searchRow as $row ) {
						if ($row) {
							$search .= "name like '%" . $row . "%' OR ";
							$search .= "mobile like '%" . $row . "%' OR ";
						}
					}
					$search = substr ( $search, 0, strlen ( $search ) - 3 );
					if ($search)
						$search .= ')';
				}
			}
			
			$data ['listMode'] = $this->uri->segment ( 4, 0 );
			$data ['page'] = $this->uri->segment ( 5, 0 );
			$data ['num'] = $this->Mmessage->getMessageNum ( $data ['listMode'], $search );
			
			$config ['base_url'] = base_url () . '/admin/message/messageList/' . $data ['listMode'] . '/';
			$config ['total_rows'] = $data ['num'];
			$config ['per_page'] = PAGE_SHOW_NUM;
			$config ['uri_segment'] = 5;
			$config ['num_links'] = 2;
			$this->pagination->initialize ( $config );
			
			$data ['list'] = $this->Mmessage->getMessageList ( $data ['listMode'], $data ['page'], PAGE_SHOW_NUM, $search );
			
			$data ['authority'] = $this->session->userdata ( 'authority' );
			$data ['adminName'] = $this->session->userdata ( 'adminName' );
			
			$this->load->view ( 'admin/header', $data );
			$this->load->view ( 'admin/nav' );
			$this->load->view ( 'admin/message_messagelist' );
			$this->load->view ( 'admin/footer' );
		} else {
			$data ['msg'] = '请先登录！';
			$data ['url'] = base_url () . 'admin/home/login';
			$this->load->view ( 'admin/admin_message', $data );
		}
	}
	
	function dealJoin() {
		if (($this->session->userdata ( 'authority' ) === '0') || ($this->session->userdata ( 'authority' ) === '1') || ($this->session->userdata ( 'authority' ) === '2')) {
			$data ['id'] = $this->uri->segment ( 4, 0 );
			$this->load->model ( 'Mmessage' );
			$data ['deal'] = $this->input->post ( 'deal' );
			$data ['state'] = 'T';
			$data ['updateTime'] = date ( "Y/m/d H:i:s", time () );
			$data ['dealPerson'] = $this->session->userdata ( 'adminName' );
			if ($this->Mmessage->updateJoin ( $data )) {
				$data ['msg'] = '处理状态更新成功！';
			} else
				$data ['msg'] = '处理状态更新失败！';
			
			$data ['url'] = base_url () . 'admin/message/joinList/2';
			$this->load->view ( 'admin/admin_message', $data );
		} else {
			$data ['msg'] = '请先登录！';
			$data ['url'] = base_url () . 'admin/home/login';
			$this->load->view ( 'admin/admin_message', $data );
		}
	}
	
	function dealReservation() {
		if (($this->session->userdata ( 'authority' ) === '0') || ($this->session->userdata ( 'authority' ) === '1') || ($this->session->userdata ( 'authority' ) === '2')) {
			$data ['id'] = $this->uri->segment ( 4, 0 );
			$this->load->model ( 'Mmessage' );
			$data ['deal'] = $this->input->post ( 'deal' );
			$data ['state'] = 'T';
			$data ['updateTime'] = date ( "Y/m/d H:i:s", time () );
			$data ['dealPerson'] = $this->session->userdata ( 'adminName' );
			if ($this->Mmessage->updateReservation ( $data )) {
				$data ['msg'] = '处理状态更新成功！';
			} else
				$data ['msg'] = '处理状态更新失败！';
			
			$data ['url'] = base_url () . 'admin/message/reservationList/2';
			$this->load->view ( 'admin/admin_message', $data );
		} else {
			$data ['msg'] = '请先登录！';
			$data ['url'] = base_url () . 'admin/home/login';
			$this->load->view ( 'admin/admin_message', $data );
		}
	}
	
	function dealMessage() {
		if (($this->session->userdata ( 'authority' ) === '0') || ($this->session->userdata ( 'authority' ) === '1') || ($this->session->userdata ( 'authority' ) === '2')) {
			$data ['id'] = $this->uri->segment ( 4, 0 );
			$this->load->model ( 'Mmessage' );
			$data ['deal'] = $this->input->post ( 'deal' );
			$data ['state'] = 'T';
			$data ['updateTime'] = date ( "Y/m/d H:i:s", time () );
			$data ['dealPerson'] = $this->session->userdata ( 'adminName' );
			if ($this->Mmessage->updateMessage ( $data )) {
				$data ['msg'] = '处理状态更新成功！';
			} else
				$data ['msg'] = '处理状态更新失败！';
			
			$data ['url'] = base_url () . 'admin/message/messageList/2';
			$this->load->view ( 'admin/admin_message', $data );
		} else {
			$data ['msg'] = '请先登录！';
			$data ['url'] = base_url () . 'admin/home/login';
			$this->load->view ( 'admin/admin_message', $data );
		}
	}
	
	function deleJoin() {
		if (($this->session->userdata ( 'authority' ) === '0') || ($this->session->userdata ( 'authority' ) === '1') || ($this->session->userdata ( 'authority' ) === '2')) {
			$data ['id'] = $this->uri->segment ( 4, 0 );
			$this->load->model ( 'Mmessage' );
			$data ['state'] = 'D';
			$data ['updateTime'] = date ( "Y/m/d H:i:s", time () );
			$data ['dealPerson'] = $this->session->userdata ( 'adminName' );
			if ($this->Mmessage->updateJoin ( $data )) {
				$data ['msg'] = '加盟申请删除成功！';
			} else
				$data ['msg'] = '加盟申请删除失败！';
			
			$data ['url'] = base_url () . 'admin/message/joinList/2';
			$this->load->view ( 'admin/admin_message', $data );
		} else {
			$data ['msg'] = '请先登录！';
			$data ['url'] = base_url () . 'admin/home/login';
			$this->load->view ( 'admin/admin_message', $data );
		}
	}
	
	function deleReservation() {
		if (($this->session->userdata ( 'authority' ) === '0') || ($this->session->userdata ( 'authority' ) === '1') || ($this->session->userdata ( 'authority' ) === '2')) {
			$data ['id'] = $this->uri->segment ( 4, 0 );
			$this->load->model ( 'Mmessage' );
			$data ['state'] = 'D';
			$data ['updateTime'] = date ( "Y/m/d H:i:s", time () );
			$data ['dealPerson'] = $this->session->userdata ( 'adminName' );
			if ($this->Mmessage->updateReservation ( $data )) {
				$data ['msg'] = '预约删除成功！';
			} else
				$data ['msg'] = '预约删除失败！';
			
			$data ['url'] = base_url () . 'admin/message/reservationList/2';
			$this->load->view ( 'admin/admin_message', $data );
		} else {
			$data ['msg'] = '请先登录！';
			$data ['url'] = base_url () . 'admin/home/login';
			$this->load->view ( 'admin/admin_message', $data );
		}
	}
	
	function deleMessage() {
		if (($this->session->userdata ( 'authority' ) === '0') || ($this->session->userdata ( 'authority' ) === '1') || ($this->session->userdata ( 'authority' ) === '2')) {
			$data ['id'] = $this->uri->segment ( 4, 0 );
			$this->load->model ( 'Mmessage' );
			$data ['state'] = 'D';
			$data ['updateTime'] = date ( "Y/m/d H:i:s", time () );
			$data ['dealPerson'] = $this->session->userdata ( 'adminName' );
			if ($this->Mmessage->updateMessage ( $data )) {
				$data ['msg'] = '留言删除成功！';
			} else
				$data ['msg'] = '留言删除失败！';
			
			$data ['url'] = base_url () . 'admin/message/messageList/2';
			$this->load->view ( 'admin/admin_message', $data );
		} else {
			$data ['msg'] = '请先登录！';
			$data ['url'] = base_url () . 'admin/home/login';
			$this->load->view ( 'admin/admin_message', $data );
		}
	}
	
	function download() {
		if ($this->session->userdata ( 'authority' ) === '0') {
			$this->form_validation->set_rules ( 'begin', '开始编号', 'trim|numeric|callback_checkBeginCB' );
			$this->form_validation->set_rules ( "end", '结束编号', 'trim|numeric|callback_checkEndCB' );
			$this->form_validation->set_rules ( "class", '留言类型', '' );
			$this->load->model ( 'Mmessage' );
			$this->load->model ( 'Mcard' );
			
			if ($this->form_validation->run () == FALSE) {
				$data ['form'] = array ('id' => 'downloadForm' );
				$data ['authority'] = $this->session->userdata ( 'authority' );
				$data ['adminName'] = $this->session->userdata ( 'adminName' );
				
				$this->load->view ( 'admin/header', $data );
				$this->load->view ( 'admin/nav' );
				$this->load->view ( 'admin/message_download' );
				$this->load->view ( 'admin/footer' );
			} else {
				$class = $this->input->post ( 'class' );
				$begin = $this->input->post ( 'begin' );
				$end = $this->input->post ( 'end' );
				
				$filePath = './uploads/business/';
				require_once 'application/libraries/PHPExcel.php';
				require_once 'application/libraries/PHPExcel/Writer/Excel2007.php';
				require_once 'application/libraries/PHPExcel/Writer/Excel5.php';
				include_once 'application/libraries/PHPExcel/IOFactory.php';
				
				$objExcel = new PHPExcel ();
				$objExcel->getProperties ()->setCreator ( "newstar" );
				$objExcel->setActiveSheetIndex ( 0 );
				
				$i = 1;
				if (! $begin)
					$begin = 1;
				
				if ($class == '0') {
					$num = $this->Mmessage->getBusinessJoinMaxNum ();
					if (! $end)
						$end = $num;
					$objExcel->getActiveSheet ()->setCellValue ( 'a1', "商户加盟信息记录表" );
					$objExcel->getActiveSheet ()->setCellValue ( 'a2', "编号" );
					$objExcel->getActiveSheet ()->setCellValue ( 'b2', "公司名" );
					$objExcel->getActiveSheet ()->setCellValue ( 'c2', "联系人" );
					$objExcel->getActiveSheet ()->setCellValue ( 'd2', "电话" );
					$objExcel->getActiveSheet ()->setCellValue ( 'e2', "备注" );
					$objExcel->getActiveSheet ()->setCellValue ( 'f2', "状态" );
					$objExcel->getActiveSheet ()->setCellValue ( 'g2', "申请时间" );
					$objExcel->getActiveSheet ()->setCellValue ( 'h2', "处理时间" );
					$objExcel->getActiveSheet ()->setCellValue ( 'i2', "处理人" );
					$objExcel->getActiveSheet ()->setCellValue ( 'j2', "处理意见" );
					$query = NULL;
					if ($end)
						$query = $this->Mmessage->getJoinListforDownload ( $begin, $end );
					
					if ($query)
						foreach ( $query as $v ) {
							$u1 = $i + 2;
							/*----------写入内容-------------*/
							$objExcel->getActiveSheet ()->setCellValue ( 'a' . $u1, $v ["id"] );
							$objExcel->getActiveSheet ()->setCellValue ( 'b' . $u1, $v ["companyName"] );
							$objExcel->getActiveSheet ()->setCellValue ( 'c' . $u1, $v ["name"] );
							$objExcel->getActiveSheet ()->setCellValue ( 'd' . $u1, $v ["mobile"] );
							$objExcel->getActiveSheet ()->setCellValue ( 'e' . $u1, $v ["details"] );
							if ($v ["state"] == 'F')
								$objExcel->getActiveSheet ()->setCellValue ( 'f' . $u1, '未处理' );
							elseif ($v ["state"] == 'T')
								$objExcel->getActiveSheet ()->setCellValue ( 'f' . $u1, '已处理' );
							elseif ($v ["state"] == 'D')
								$objExcel->getActiveSheet ()->setCellValue ( 'f' . $u1, '已删除' );
							$objExcel->getActiveSheet ()->setCellValue ( 'g' . $u1, $v ["creatTime"] );
							$objExcel->getActiveSheet ()->setCellValue ( 'h' . $u1, $v ["updateTime"] );
							$objExcel->getActiveSheet ()->setCellValue ( 'i' . $u1, $v ["dealPerson"] );
							$objExcel->getActiveSheet ()->setCellValue ( 'j' . $u1, $v ["deal"] );
							$i ++;
						}
					
					$objExcel->getActiveSheet ()->getColumnDimension ( 'A' )->setWidth ( 6 );
					$objExcel->getActiveSheet ()->getColumnDimension ( 'B' )->setWidth ( 18 );
					$objExcel->getActiveSheet ()->getColumnDimension ( 'C' )->setWidth ( 10 );
					$objExcel->getActiveSheet ()->getColumnDimension ( 'D' )->setWidth ( 10 );
					$objExcel->getActiveSheet ()->getColumnDimension ( 'E' )->setWidth ( 30 );
					$objExcel->getActiveSheet ()->getColumnDimension ( 'F' )->setWidth ( 6 );
					$objExcel->getActiveSheet ()->getColumnDimension ( 'G' )->setWidth ( 18 );
					$objExcel->getActiveSheet ()->getColumnDimension ( 'H' )->setWidth ( 18 );
					$objExcel->getActiveSheet ()->getColumnDimension ( 'I' )->setWidth ( 10 );
					$objExcel->getActiveSheet ()->getColumnDimension ( 'J' )->setWidth ( 10 );
					
					$timestamp = time ();
					$objExcel->getActiveSheet ()->mergeCells ( 'A1:J1' );
					$objExcel->getActiveSheet ()->getStyle ( 'A1:J1' )->getAlignment ()->setHorizontal ( PHPExcel_Style_Alignment::HORIZONTAL_CENTER );
					
					header ( 'Content-Type: application/vnd.ms-excel' );
					header ( 'Content-Disposition: attachment;filename="businessjoin_' . $timestamp . '.xls"' );
					header ( 'Cache-Control: max-age=0' );
				} elseif ($class == '1') {
					$num = $this->Mmessage->getMessageMaxNum ();
					if (! $end)
						$end = $num;
					$objExcel->getActiveSheet ()->setCellValue ( 'a1', "用户留言信息记录表" );
					$objExcel->getActiveSheet ()->setCellValue ( 'a2', "编号" );
					$objExcel->getActiveSheet ()->setCellValue ( 'b2', "用户名" );
					$objExcel->getActiveSheet ()->setCellValue ( 'c2', "电话" );
					$objExcel->getActiveSheet ()->setCellValue ( 'd2', "内容" );
					$objExcel->getActiveSheet ()->setCellValue ( 'e2', "状态" );
					$objExcel->getActiveSheet ()->setCellValue ( 'f2', "留言时间" );
					$objExcel->getActiveSheet ()->setCellValue ( 'g2', "处理时间" );
					$objExcel->getActiveSheet ()->setCellValue ( 'h2', "处理人" );
					$objExcel->getActiveSheet ()->setCellValue ( 'i2', "处理意见" );
					$query = NULL;
					if ($end)
						$query = $this->Mmessage->getMessageListforDownload ( $begin, $end );
					if ($query)
						foreach ( $query as $v ) {
							$u1 = $i + 2;
							/*----------写入内容-------------*/
							$objExcel->getActiveSheet ()->setCellValue ( 'a' . $u1, $v ["id"] );
							$objExcel->getActiveSheet ()->setCellValue ( 'b' . $u1, $v ["name"] );
							$objExcel->getActiveSheet ()->setCellValue ( 'c' . $u1, $v ["mobile"] );
							$objExcel->getActiveSheet ()->setCellValue ( 'd' . $u1, $v ["details"] );
							if ($v ["state"] == 'F')
								$objExcel->getActiveSheet ()->setCellValue ( 'e' . $u1, '未处理' );
							elseif ($v ["state"] == 'T')
								$objExcel->getActiveSheet ()->setCellValue ( 'e' . $u1, '已处理' );
							elseif ($v ["state"] == 'D')
								$objExcel->getActiveSheet ()->setCellValue ( 'e' . $u1, '已删除' );
							$objExcel->getActiveSheet ()->setCellValue ( 'f' . $u1, $v ["creatTime"] );
							$objExcel->getActiveSheet ()->setCellValue ( 'g' . $u1, $v ["updateTime"] );
							$objExcel->getActiveSheet ()->setCellValue ( 'h' . $u1, $v ["dealPerson"] );
							$objExcel->getActiveSheet ()->setCellValue ( 'i' . $u1, $v ["deal"] );
							$i ++;
						}
					
					$objExcel->getActiveSheet ()->getColumnDimension ( 'A' )->setWidth ( 6 );
					$objExcel->getActiveSheet ()->getColumnDimension ( 'B' )->setWidth ( 10 );
					$objExcel->getActiveSheet ()->getColumnDimension ( 'C' )->setWidth ( 10 );
					$objExcel->getActiveSheet ()->getColumnDimension ( 'D' )->setWidth ( 30 );
					$objExcel->getActiveSheet ()->getColumnDimension ( 'E' )->setWidth ( 6 );
					$objExcel->getActiveSheet ()->getColumnDimension ( 'F' )->setWidth ( 18 );
					$objExcel->getActiveSheet ()->getColumnDimension ( 'G' )->setWidth ( 18 );
					$objExcel->getActiveSheet ()->getColumnDimension ( 'H' )->setWidth ( 10 );
					$objExcel->getActiveSheet ()->getColumnDimension ( 'I' )->setWidth ( 10 );
					
					$timestamp = time ();
					$objExcel->getActiveSheet ()->mergeCells ( 'A1:I1' );
					$objExcel->getActiveSheet ()->getStyle ( 'A1:I1' )->getAlignment ()->setHorizontal ( PHPExcel_Style_Alignment::HORIZONTAL_CENTER );
					
					header ( 'Content-Type: application/vnd.ms-excel' );
					header ( 'Content-Disposition: attachment;filename="message_' . $timestamp . '.xls"' );
					header ( 'Cache-Control: max-age=0' );
				} elseif ($class == '2') {
					$num = $this->Mmessage->getReservationMaxNum ();
					if (! $end)
						$end = $num;
					$objExcel->getActiveSheet ()->setCellValue ( 'a1', "用户预约信息记录表" );
					$objExcel->getActiveSheet ()->setCellValue ( 'a2', "编号" );
					$objExcel->getActiveSheet ()->setCellValue ( 'b2', "用户名" );
					$objExcel->getActiveSheet ()->setCellValue ( 'c2', "电话" );
					$objExcel->getActiveSheet ()->setCellValue ( 'd2', "状态" );
					$objExcel->getActiveSheet ()->setCellValue ( 'e2', "申请时间" );
					$objExcel->getActiveSheet ()->setCellValue ( 'f2', "预约时间" );
					$objExcel->getActiveSheet ()->setCellValue ( 'g2', "处理时间" );
					$objExcel->getActiveSheet ()->setCellValue ( 'h2', "处理人" );
					$objExcel->getActiveSheet ()->setCellValue ( 'i2', "处理意见" );
					$query = NULL;
					if ($end)
						$query = $this->Mmessage->getReservationListforDownload ( $begin, $end );
					if ($query)
						foreach ( $query as $v ) {
							$u1 = $i + 2;
							
							$objExcel->getActiveSheet ()->setCellValue ( 'a' . $u1, $v ["id"] );
							$objExcel->getActiveSheet ()->setCellValue ( 'b' . $u1, $v ["name"] );
							$objExcel->getActiveSheet ()->setCellValue ( 'c' . $u1, $v ["mobile"] );
							if ($v ["state"] == 'F')
								$objExcel->getActiveSheet ()->setCellValue ( 'd' . $u1, '未处理' );
							elseif ($v ["state"] == 'T')
								$objExcel->getActiveSheet ()->setCellValue ( 'd' . $u1, '已处理' );
							elseif ($v ["state"] == 'D')
								$objExcel->getActiveSheet ()->setCellValue ( 'd' . $u1, '已删除' );
							$objExcel->getActiveSheet ()->setCellValue ( 'e' . $u1, $v ["creatTime"] );
							$objExcel->getActiveSheet ()->setCellValue ( 'f' . $u1, $v ["reserveTime"] );
							$objExcel->getActiveSheet ()->setCellValue ( 'g' . $u1, $v ["updateTime"] );
							$objExcel->getActiveSheet ()->setCellValue ( 'h' . $u1, $v ["dealPerson"] );
							$objExcel->getActiveSheet ()->setCellValue ( 'i' . $u1, $v ["deal"] );
							$i ++;
						}
					
					$objExcel->getActiveSheet ()->getColumnDimension ( 'A' )->setWidth ( 6 );
					$objExcel->getActiveSheet ()->getColumnDimension ( 'B' )->setWidth ( 10 );
					$objExcel->getActiveSheet ()->getColumnDimension ( 'C' )->setWidth ( 10 );
					$objExcel->getActiveSheet ()->getColumnDimension ( 'D' )->setWidth ( 30 );
					$objExcel->getActiveSheet ()->getColumnDimension ( 'E' )->setWidth ( 6 );
					$objExcel->getActiveSheet ()->getColumnDimension ( 'F' )->setWidth ( 18 );
					$objExcel->getActiveSheet ()->getColumnDimension ( 'G' )->setWidth ( 18 );
					$objExcel->getActiveSheet ()->getColumnDimension ( 'H' )->setWidth ( 18 );
					$objExcel->getActiveSheet ()->getColumnDimension ( 'I' )->setWidth ( 10 );
					
					$timestamp = time ();
					$objExcel->getActiveSheet ()->mergeCells ( 'A1:I1' );
					$objExcel->getActiveSheet ()->getStyle ( 'A1:I1' )->getAlignment ()->setHorizontal ( PHPExcel_Style_Alignment::HORIZONTAL_CENTER );
					
					header ( 'Content-Type: application/vnd.ms-excel' );
					header ( 'Content-Disposition: attachment;filename="reservation_' . $timestamp . '.xls"' );
					header ( 'Cache-Control: max-age=0' );
				} elseif ($class == '3') {
					$num = $this->Mcard->getCardAppMaxNum ();
					if (! $end)
						$end = $num;
					$objExcel->getActiveSheet ()->setCellValue ( 'a1', "卡片申购信息记录表" );
					$objExcel->getActiveSheet ()->setCellValue ( 'a2', "编号" );
					$objExcel->getActiveSheet ()->setCellValue ( 'b2', "公司名" );
					$objExcel->getActiveSheet ()->setCellValue ( 'c2', "联系人" );
					$objExcel->getActiveSheet ()->setCellValue ( 'd2', "电话" );
					$objExcel->getActiveSheet ()->setCellValue ( 'e2', "公司地址" );
					$objExcel->getActiveSheet ()->setCellValue ( 'f2', "面额1" );
					$objExcel->getActiveSheet ()->setCellValue ( 'g2', "张数1" );
					$objExcel->getActiveSheet ()->setCellValue ( 'h2', "面额2" );
					$objExcel->getActiveSheet ()->setCellValue ( 'i2', "张数2" );
					$objExcel->getActiveSheet ()->setCellValue ( 'j2', "面额3" );
					$objExcel->getActiveSheet ()->setCellValue ( 'k2', "张数3" );
					$objExcel->getActiveSheet ()->setCellValue ( 'l2', "面额4" );
					$objExcel->getActiveSheet ()->setCellValue ( 'm2', "张数4" );
					$objExcel->getActiveSheet ()->setCellValue ( 'n2', "申购时间" );
					$objExcel->getActiveSheet ()->setCellValue ( 'o2', "处理时间" );
					$objExcel->getActiveSheet ()->setCellValue ( 'p2', "处理人" );
					$objExcel->getActiveSheet ()->setCellValue ( 'q2', "状态" );
					$objExcel->getActiveSheet ()->setCellValue ( 'r2', "处理意见" );
					$query = NULL;
					if ($end)
						$query = $this->Mcard->getAppListforDownload ( $begin, $end );
					if ($query)
						foreach ( $query as $v ) {
							$u1 = $i + 2;
							/*----------写入内容-------------*/
							$objExcel->getActiveSheet ()->setCellValue ( 'a' . $u1, $v ["id"] );
							$objExcel->getActiveSheet ()->setCellValue ( 'b' . $u1, $v ["companyName"] );
							$objExcel->getActiveSheet ()->setCellValue ( 'c' . $u1, $v ["name"] );
							$objExcel->getActiveSheet ()->setCellValue ( 'd' . $u1, $v ["phone"] );
							$objExcel->getActiveSheet ()->setCellValue ( 'e' . $u1, $v ["address"] );
							$objExcel->getActiveSheet ()->setCellValue ( 'f' . $u1, $v ["denomination1"] );
							$objExcel->getActiveSheet ()->setCellValue ( 'g' . $u1, $v ["number1"] );
							$objExcel->getActiveSheet ()->setCellValue ( 'h' . $u1, $v ["denomination2"] );
							$objExcel->getActiveSheet ()->setCellValue ( 'i' . $u1, $v ["number2"] );
							$objExcel->getActiveSheet ()->setCellValue ( 'j' . $u1, $v ["denomination3"] );
							$objExcel->getActiveSheet ()->setCellValue ( 'k' . $u1, $v ["number3"] );
							$objExcel->getActiveSheet ()->setCellValue ( 'l' . $u1, $v ["denomination4"] );
							$objExcel->getActiveSheet ()->setCellValue ( 'm' . $u1, $v ["number4"] );
							$objExcel->getActiveSheet ()->setCellValue ( 'n' . $u1, $v ["creatTime"] );
							$objExcel->getActiveSheet ()->setCellValue ( 'o' . $u1, $v ["updateTime"] );
							$objExcel->getActiveSheet ()->setCellValue ( 'p' . $u1, $v ["updatePerson"] );
							if ($v ["state"] == 'F')
								$objExcel->getActiveSheet ()->setCellValue ( 'q' . $u1, '未处理' );
							elseif ($v ["state"] == 'T')
								$objExcel->getActiveSheet ()->setCellValue ( 'q' . $u1, '已处理' );
							elseif ($v ["state"] == 'D')
								$objExcel->getActiveSheet ()->setCellValue ( 'q' . $u1, '已删除' );
							$objExcel->getActiveSheet ()->setCellValue ( 'r' . $u1, $v ["deal"] );
							$i ++;
						}
					
					$objExcel->getActiveSheet ()->getColumnDimension ( 'A' )->setWidth ( 6 );
					$objExcel->getActiveSheet ()->getColumnDimension ( 'B' )->setWidth ( 10 );
					$objExcel->getActiveSheet ()->getColumnDimension ( 'C' )->setWidth ( 10 );
					$objExcel->getActiveSheet ()->getColumnDimension ( 'D' )->setWidth ( 10 );
					$objExcel->getActiveSheet ()->getColumnDimension ( 'E' )->setWidth ( 10 );
					$objExcel->getActiveSheet ()->getColumnDimension ( 'F' )->setWidth ( 10 );
					$objExcel->getActiveSheet ()->getColumnDimension ( 'G' )->setWidth ( 10 );
					$objExcel->getActiveSheet ()->getColumnDimension ( 'H' )->setWidth ( 10 );
					$objExcel->getActiveSheet ()->getColumnDimension ( 'I' )->setWidth ( 10 );
					$objExcel->getActiveSheet ()->getColumnDimension ( 'J' )->setWidth ( 10 );
					$objExcel->getActiveSheet ()->getColumnDimension ( 'K' )->setWidth ( 10 );
					$objExcel->getActiveSheet ()->getColumnDimension ( 'L' )->setWidth ( 10 );
					$objExcel->getActiveSheet ()->getColumnDimension ( 'M' )->setWidth ( 10 );
					$objExcel->getActiveSheet ()->getColumnDimension ( 'N' )->setWidth ( 10 );
					$objExcel->getActiveSheet ()->getColumnDimension ( 'O' )->setWidth ( 10 );
					$objExcel->getActiveSheet ()->getColumnDimension ( 'P' )->setWidth ( 10 );
					$objExcel->getActiveSheet ()->getColumnDimension ( 'Q' )->setWidth ( 10 );
					$objExcel->getActiveSheet ()->getColumnDimension ( 'R' )->setWidth ( 10 );
					
					$timestamp = time ();
					$objExcel->getActiveSheet ()->mergeCells ( 'A1:R1' );
					$objExcel->getActiveSheet ()->getStyle ( 'A1:R1' )->getAlignment ()->setHorizontal ( PHPExcel_Style_Alignment::HORIZONTAL_CENTER );
					
					header ( 'Content-Type: application/vnd.ms-excel' );
					header ( 'Content-Disposition: attachment;filename="cardApplication_' . $timestamp . '.xls"' );
					header ( 'Cache-Control: max-age=0' );
				}
				
				$objWriter = PHPExcel_IOFactory::createWriter ( $objExcel, 'Excel5' );
				$objWriter->save ( 'php://output' );
			}
		} else {
			if ($this->session->userdata ( 'adminName' )) {
				$data ['msg'] = '您没有群发邮件的权限！';
				$data ['url'] = base_url () . 'admin/home';
				$this->load->view ( 'admin/admin_message', $data );
			} else {
				$data ['msg'] = '请先登录！';
				$data ['url'] = base_url () . 'admin/home/login';
				$this->load->view ( 'admin/admin_message', $data );
			}
		}
	}
	
	function checkBeginCB($str) {
		if ($str) {
			$this->load->model ( 'Mmessage' );
			$class = $this->input->post ( 'class' );
			if ($class == '0')
				$num = $this->Mmessage->getBusinessJoinMaxNum ();
			elseif ($class == '1')
				$num = $this->Mmessage->getMessageMaxNum ();
			elseif ($class == '2')
				$num = $this->Mmessage->getReservationMaxNum ();
			if ($str > 0 && $str <= $this->input->post ( 'end' ) && $str <= $num)
				return TRUE;
			else {
				$this->form_validation->set_message ( 'checkBeginCB', '%s不正确' );
				return FALSE;
			}
		} else
			return TRUE;
	}
	
	function checkEndCB($str) {
		if ($str) {
			$this->load->model ( 'Mmessage' );
			$class = $this->input->post ( 'class' );
			if ($class == '0')
				$num = $this->Mmessage->getBusinessJoinMaxNum ();
			elseif ($class == '1')
				$num = $this->Mmessage->getMessageMaxNum ();
			elseif ($class == '2')
				$num = $this->Mmessage->getReservationMaxNum ();
			if ($str > 0 && $str >= $this->input->post ( 'begin' ) && $str <= $num)
				return TRUE;
			else {
				$this->form_validation->set_message ( 'checkEndCB', '%s不正确' );
				return FALSE;
			}
		} else
			return TRUE;
	}
}